package com.vng.zini.gifapp.activity;

import java.util.ArrayList;

import com.vng.zini.gifapp.components.Images;
import com.vng.zini.gifapp.config.AppParameters;
import com.vng.zini.gifapp.utils.CreateGIF;
import com.vng.zini.giffapp.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class GIFPreviewActivity extends Activity{
	SeekBar seekbar;
	
	ImageButton next, back;
	ArrayList<Bitmap> lBitmap;
	Worker1 worker1;
	int progressSeekBar;
	Context context = this;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		System.out.println("on create gif preview activity");
		setContentView(R.layout.preview_gif);
		setListenerOnSeekBar();
		setListenerOnButton();
		
		lBitmap = Images.listImageChecked;
		worker1 = new Worker1();
		worker1.start();
		
		
	}
	private void setListenerOnButton(){
		
		next = (ImageButton) findViewById(R.id.topbar_black_right_btn_gif_preview);
		back = (ImageButton) findViewById(R.id.topbar_black_left_btn_gif_preview);
		next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final ProgressDialog progressDialog = ProgressDialog.show(GIFPreviewActivity.this, "", "Đang tạo ảnh GIF ...");
				
				//CreateGIF.create(context, lBitmap);
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						CreateGIF.create(context, lBitmap);
						progressDialog.dismiss();
						
					}
				}).start();
				
			}
		});
		
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				//Intent intent = new Intent(PreviewGIF.this, Test_ImagePreview2.class);
				//startActivity(intent);
				finish();
			}
		});
	}
	private void setListenerOnSeekBar(){
		seekbar = (SeekBar) findViewById(R.id.seek_preview);
		seekbar.setProgress(50);
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
 
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
            	progressSeekBar = progress;
            }
 
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
 
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }
        });
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(!worker1.isInterrupted()){
			worker1.interrupt();
		}
	}
	
	public class Worker1 extends Thread{
		public void run(){
			// TODO Auto-generated method stub
			int i=0;
			final ImageView image = (ImageView) findViewById(R.id.gif_preview);
			while(!Thread.currentThread().isInterrupted()){
				final int image_index = i % AppParameters.numberFramesChecked;
				image.post(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Bitmap bm = lBitmap.get(image_index);
						image.setImageBitmap(bm);
					}
				});
				
				try {
					Thread.sleep(250 - progressSeekBar);
				} catch (InterruptedException e) { 
					// TODO Auto-generated catch block
					e.printStackTrace();
					Thread.currentThread().interrupt();
				}
				i++;
			}
			
		}
	}

	
	
}
